package com.phifan.book_zdmm;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.domob.android.ads.DomobAdManager;
import cn.domob.android.ads.DomobAdView;

import com.phifan.book_zdmm.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.util.zdmm.*;
public class MainActivity extends Activity {

	private static final String TEMP = "temp";
	private PageWidget mPageWidget;
	Bitmap mCurPageBitmap, mNextPageBitmap;
	Canvas mCurPageCanvas, mNextPageCanvas;
	BookPageFactory pagefactory;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);	
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mPageWidget = new PageWidget(this);
				
	//	setContentView(mPageWidget);
		setContentView(R.layout.mainpage);
		RelativeLayout mainLinear = (RelativeLayout)findViewById(R.id.up);
	//	LinearLayout mainLinear = new LinearLayout(this);
	//	mainLinear.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 
	//			LinearLayout.LayoutParams.FILL_PARENT));
	//	mainLinear.setOrientation(LinearLayout.VERTICAL);
	//	setContentView(mainLinear);
		
		mainLinear.addView(mPageWidget);
		DomobAdView adview = new DomobAdView(this);	
		
		DomobAdManager.setPublisherId("56OJzwqouM1b4Z6g4r");
		adview.setRequestInterval(20);	
		
	//	mPageWidget = (com.util.PageWidget)findViewById(R.id.widget);
		RelativeLayout downLinear = (RelativeLayout)findViewById(R.id.down);
	//	downLinear.addView(adview, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		downLinear.addView(adview, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		
		mCurPageBitmap = Bitmap.createBitmap(ScreenManager.getScreen().width, ScreenManager.getScreen().height, Bitmap.Config.ARGB_8888);
		mNextPageBitmap = Bitmap
				.createBitmap(ScreenManager.getScreen().width, ScreenManager.getScreen().height, Bitmap.Config.ARGB_8888);

		mCurPageCanvas = new Canvas(mCurPageBitmap);
		mNextPageCanvas = new Canvas(mNextPageBitmap);
		pagefactory = new BookPageFactory(ScreenManager.getScreen().width, ScreenManager.getScreen().height);

		pagefactory.setBgBitmap(BitmapFactory.decodeResource(
				this.getResources(), ScreenManager.getScreen().bg_int));
		
		//读取上次读到的 地方
		SharedPreferences sp = getSharedPreferences(TEMP, MODE_WORLD_READABLE);
	    int buffEnd = sp.getInt("buffEnd", -1); 	
	    if(buffEnd != -1){
	    	pagefactory.setStart(buffEnd);
	    }
		
		try {			
			byte[] buff = new byte[1024];
			InputStream is = getAssets().open("book.txt");
			File file = new File("/sdcard/zangdimima.txt");
			if(!file.exists()) {file.createNewFile();}
			
			FileOutputStream fos = new FileOutputStream("/sdcard/zangdimima.txt");
			int len = 0;
			while(true) {			
				len = is.read(buff, 0, 1024);
				if(len == -1) break;
				fos.write(buff, 0, len);
			}				
			pagefactory.openbook("/sdcard/zangdimima.txt");
			pagefactory.onDraw(mCurPageCanvas);
		} catch (IOException e1) {
			Toast.makeText(this, "电子书不存在,请将《zangdimima.txt》放在SD卡根目录下",
					Toast.LENGTH_LONG).show();
		}

		mPageWidget.setBitmaps(mCurPageBitmap, mCurPageBitmap);

		mPageWidget.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub

				boolean ret = false;
				if (v == mPageWidget) {
					if (e.getAction() == MotionEvent.ACTION_DOWN) {
						mPageWidget.abortAnimation();
						mPageWidget.calcCornerXY(e.getX(), e.getY());

						pagefactory.onDraw(mCurPageCanvas);
						if (mPageWidget.DragToRight()) {
							try {
								pagefactory.prePage();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							if (pagefactory.isfirstPage())
								return false;
							pagefactory.onDraw(mNextPageCanvas);
						} else {
							try {
								pagefactory.nextPage();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							if (pagefactory.islastPage()) {
								return false;
							}
							pagefactory.onDraw(mNextPageCanvas);
						}
						mPageWidget.setBitmaps(mCurPageBitmap, mNextPageBitmap);
					}

					ret = mPageWidget.doTouchEvent(e);
					return ret;
				}

				return false;
			}

		});
	}	
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
        	
        	//保存
        	SharedPreferences.Editor localEditor1 = getSharedPreferences(TEMP,
    				MODE_WORLD_WRITEABLE).edit();
    		localEditor1.putInt("buffEnd", pagefactory.getEnd());		
    		localEditor1.commit();
        	
        	
        	android.os.Process.killProcess(android.os.Process.myPid()); 
        	System.exit(0);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
}
